/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#ifndef DEV_PLDM_PROTOCOL_H
#define DEV_PLDM_PROTOCOL_H


#ifdef __cplusplus
extern "C" {
#endif


#include <sys/ioctl.h>

#ifdef BD_BIG_ENDIAN
    #define PLDM_LE16_TO_CPU(s) ((((s) & 0xFF) << 8) | (((s) >> 8) & 0xFF))
    #define PLDM_LE32_TO_CPU(l) ((((l) & 0xFF) << 24) | \
                                (((l) & 0xFF00) << 8) | \
                                (((l) >> 8) & 0xFF00) | \
                                (((l) >> 24) & 0xFF))
    #define PLDM_CPU_TO_LE16(s) PLDM_LE16_TO_CPU(s)
    #define PLDM_CPU_TO_LE32(l) PLDM_LE32_TO_CPU(l)
    #define PLDM_BE16_TO_CPU(s) (s)
    #define PLDM_BE32_TO_CPU(l) (l)
    
    #define PLDM_CPU_TO_BE16(s) (s)
    #define PLDM_CPU_TO_BE32(l) (l)
#else
    #define PLDM_LE16_TO_CPU(s) (s)
    #define PLDM_LE32_TO_CPU(l) (l)
    #define PLDM_CPU_TO_LE16(s) (s)
    #define PLDM_CPU_TO_LE32(l) (l)
    #define PLDM_BE16_TO_CPU(s) ((((s) & 0xFF) << 8) | (((s) >> 8) & 0xFF))
    #define PLDM_BE32_TO_CPU(l) ((((l) & 0xFF) << 24) | \
                                (((l) & 0xFF00) << 8) | \
                                (((l) >> 8) & 0xFF00) | \
                                (((l) >> 24) & 0xFF))
    
    #define PLDM_CPU_TO_BE16(s) PLDM_BE16_TO_CPU(s)
    #define PLDM_CPU_TO_BE32(l) PLDM_BE32_TO_CPU(l)
#endif


#define MCTP_PLDM_TYPE_2 0x02
#define MCTP_PLDM_SENSOR_OPERATION_STATE_ENABLE 0x00
#define MCTP_EMULEX_SENSOR_STATE_CONNECT 0x01

#define PLDM_GET_SENSOR_READING 0x11
#define PLDM_GET_STATE_SENSOR_READINGS 0x21

#define MCTP_PLDM_TYPE_4 0x04
#define MCTP_PLDM_CMD_GET_RECORD_TABLE 0x02



#pragma pack(1)
typedef struct pldm_sensor_reading_rsp {
    uint8_t sensor_data_size;
    uint8_t sensor_operational_state;
    uint8_t event_message_enable;
    uint8_t present_state;
    uint8_t previous_state;
    uint8_t event_state;
    uint8_t sensor_data[1];
} pldm_sensor_reading_rsp_t;



typedef struct pldm_state_sensor_reading_rsp {
    uint8_t composite_sensor_count;
} pldm_state_sensor_reading_rsp_t;

// State sensor field structure
typedef struct pldm_statesensor_field {
    uint8_t operational_state;  
    uint8_t present_state; 
    uint8_t previous_state; 
    uint8_t event_state; 
} pldm_statesensor_field_t;
#pragma pack()

#ifdef __cplusplus
}
#endif

#endif